var riderMarkers = [];
var storeMarkers = [];
var userMarkers = [];
var startMarkers = [];
// 归类点位的所属地图
function sortOutPointBelongMap() {
    console.log("currentViewMapType  0为航站楼，1为卫星厅", currentViewMapType, allMaps);
    if ($parent_this) {
        let scsData = [];
        let rcsData = [];
        $parent_this.dataList.forEach((f, index) => {
            scsData.push({
                ...latlngToMapXyCoords({
                    lng: f.shopLng,
                    lat: f.shopLat,
                }),
                level: f.shopLevel,
                extra: f
            });

        });
        if (scsData.length) {
            initStoreLayer([scsData[scsData.length - 1]]);
        }
        $parent_this.riderList.forEach((f, index) => {
            rcsData.push({
                ...latlngToMapXyCoords({
                    lng: f.riderLng,
                    lat: f.riderLat
                }),
                level: f.riderLevel || 1,
                extra: f
            });
        });
        initRiderLayer(rcsData);
    }
}

function initRiderLayer(data) {
    // const riderCoords = [
    //     {
    //         ...latlngToMapXyCoords({
    //             lng: 121.79967448316701,
    //             lat: 31.15246233922889
    //         }),
    //         level: 1,
    //         extra: {
    //             id: 'rider1',
    //         }
    //     },
    //     {
    //         x: 13559017.206499994,
    //         y: 3652707.198985274,
    //         level: 2,
    //         extra: {
    //             id: 'rider2',
    //         }
    //     },
    //     {
    //         x: 13558745.319601636,
    //         y: 3652435.3050997257,
    //         level: 2,
    //         extra: {
    //             id: 'rider3',
    //         }
    //     }
    // ]
    addRiderMarkers(data)
}

function initStoreLayer(data) {
    // const storeCoords = [
    //     {
    //         x: 13558909.281416168,
    //         y: 3652497.115374435,
    //         level: 4,
    //         extra: {
    //             id: 'store1',
    //             shopName: '麦当劳',
    //         }
    //     },
    //     {
    //         x: 13558650.320689749,
    //         y: 3652428.891250141,
    //         level: 6,
    //         extra: {
    //             id: 'store2',
    //             shopName: '歌德酒行',
    //         }
    //     },
    //     {
    //         ...latlngToMapXyCoords({
    //             lng: 121.80285638244966,
    //             lat: 31.154321238882464
    //         }),
    //         level: 2,
    //         extra: {
    //             id: 'store3',
    //             shopName: '意大利奶奶面包店',

    //         }
    //     }
    // ]
    addStoreMarkers(data)
}



function initUserLayer(data) {
    // const userCoords = [
    //     {
    //         x: 13558909.281416168,
    //         y: 3652497.115374435,
    //         level: 4,
    //         extra: {
    //             id: 'store1',
    //             shopName: '麦当劳',
    //         }
    //     },
    //     {
    //         x: 13558650.320689749,
    //         y: 3652428.891250141,
    //         level: 6,
    //         extra: {
    //             id: 'store2',
    //             shopName: '歌德酒行',
    //         }
    //     },
    //     {
    //         ...latlngToMapXyCoords({
    //             lng: 121.80285638244966,
    //             lat: 31.154321238882464
    //         }),
    //         level: 2,
    //         extra: {
    //             id: 'store3',
    //             shopName: '意大利奶奶面包店',

    //         }
    //     }
    // ]
    addUserMarkers(data)
}

function initStartLayer(data) {
    addStartMarkers(data)
}


/* 构造骑手 Marker 并添加到地图上 */
function addRiderMarkers(data) {
    // riderMarkers = data.map(feature => {
    //     var panelSet1 = {
    //         ...panelSet,
    //         x: feature.x,
    //         y: feature.y,
    //         nodeType: "riderPoint",
    //         extra: feature.extra
    //     }
    //     if (panelSet.auto === 'on') {
    //         delete panelSet1.domWidth
    //         delete panelSet1.domHeight
    //     }
    //     if (!feature.extra) {
    //         feature.extra = ''
    //     }
    //     let sourceJson = Qs.stringify(panelSet1);
    //     let marker = new fengmap.FMDomMarker({
    //         ...panelSet1,
    //         anchor: anchorObj[panelSet1.anchor],
    //         content: `
    //             <div class="rider-marker-body" onclick="handleMarkerEvent('${sourceJson}')">
    //               <div class="message-box">
    //                 <div class="message-container">
    //                   <div class="message-top">
    //                     <div class="rider-name">
    //                         ${feature.extra.name}
    //                     </div>
    //                     <div class="rider-floor">
    //                         ${feature.extra.addr || feature.extra.phone}
    //                     </div>
    //                   </div>

    //                   <div class="message-bottom">
    //                     <div class="today-order-num">
    //                       今日已接单${feature.extra.orderCount}单
    //                     </div>
    //                     <div class="message-bottom-line">
    //                     </div>
    //                     <div class="deliverying-num">
    //                       配送中${feature.extra.deliveryOrderCount}单
    //                     </div>
    //                   </div>
    //                  </div>
    //               </div>
    //               <img class="map-rider-icon" src="./static/map-rider-icon.png" />
    //             </div>
    //   `
    //     })
    //     marker._extra = feature.extra;// 附带上当前点位的基本信息，可能其他地方需要对应关系
    //     var level = feature.level || map.getLevel()
    //     catchLevelPossibleException();
    //     var floor = map.getFloor(level)
    //     marker.addTo(floor)
    //     return marker
    // });

    riderMarkers = data.map((feature, index) => {
        /* 构造 Marker */
        let marker = new fengmap.FMDynamicModel({
            url: `${location.origin}/fengMap/static/riderGlb/Ren_${index + 1}.glb`,
            x: feature.x,
            y: feature.y,
            id: feature,
            height: 1,
            // color: '#ffffff',
            scale: 24,
            heading: 0,
            fadeIn: false,
            fadeOut: false,
            callback: function () {
                console.log('模型加载完成。');
            }
        });
        marker._extra = feature.extra;// 附带上当前点位的基本信息，可能其他地方需要对应关系
        marker.nodeType = 'riderPoint'
        var level = feature.level || map.getLevel()
        catchLevelPossibleException();
        var floor = map.getFloor(level);
        marker.addTo(floor);
        return marker
    })

}

/* 构造商户 Marker 并添加到地图上 */
function addStoreMarkers(data) {
    storeMarkers = data.map(feature => {
        var panelSet1 = {
            ...panelSet,
            x: feature.x,
            y: feature.y,
            nodeType: "storePoint",
            extra: feature.extra
        }
        if (panelSet.auto === 'on') {
            delete panelSet1.domWidth
            delete panelSet1.domHeight
        }
        if (!feature.extra) {
            feature.extra = ''
        }
        let sourceJson = Qs.stringify(panelSet1);
        let marker = new fengmap.FMDomMarker({
            ...panelSet1,
            anchor: anchorObj[panelSet1.anchor],
            content: `
                <div class="store-marker-body" onclick="handleMarkerEvent('${sourceJson}')">
                  <div class="message-box">
                    <div class="message-container">
                      ${feature.extra.shopName}(${feature.extra.shopAddress})
                    </div>
                  </div>
                  <img class="map-store-icon" src="./static/map-store-icon.png" />
                </div>
        `
        })
        marker._extra = feature.extra;// 附带上当前点位的基本信息，可能其他地方需要对应关系
        var level = feature.level || map.getLevel()
        catchLevelPossibleException();
        var floor = map.getFloor(level)
        marker.addTo(floor)
        return marker
    });
}


/* 构造用户收货 Marker 并添加到地图上 */
function addUserMarkers(data) {
    userMarkers = data.map(feature => {
        var panelSet1 = {
            ...panelSet,
            x: feature.x,
            y: feature.y,
            nodeType: "userPoint",
            extra: feature.extra
        }
        if (panelSet.auto === 'on') {
            delete panelSet1.domWidth
            delete panelSet1.domHeight
        }
        if (!feature.extra) {
            feature.extra = ''
        }
        let sourceJson = Qs.stringify(panelSet1);
        let marker = new fengmap.FMDomMarker({
            ...panelSet1,
            anchor: anchorObj[panelSet1.anchor],
            content: `
                <div class="user-marker-body" onclick="handleMarkerEvent('${sourceJson}')">
                  <div class="message-box">
                    <div class="message-container">
                      ${feature.extra.nickName}
                    </div>
                  </div>
                  <img class="map-user-icon" src="./static/dest-map-icon.png" />
                </div>
        `
        })
        marker._extra = feature.extra;// 附带上当前点位的基本信息，可能其他地方需要对应关系
        var level = feature.level || map.getLevel()
        catchLevelPossibleException();
        var floor = map.getFloor(level)
        marker.addTo(floor)
        return marker
    });
}

/* 构造正在配送中的商户作为起点 Marker 并添加到地图上 */
function addStartMarkers(data) {
    startMarkers = data.map(feature => {
        var panelSet1 = {
            ...panelSet,
            x: feature.x,
            y: feature.y,
            nodeType: "startPoint",
            extra: feature.extra
        }
        if (panelSet.auto === 'on') {
            delete panelSet1.domWidth
            delete panelSet1.domHeight
        }
        if (!feature.extra) {
            feature.extra = ''
        }
        let sourceJson = Qs.stringify(panelSet1);
        let marker = new fengmap.FMDomMarker({
            ...panelSet1,
            anchor: anchorObj[panelSet1.anchor],
            content: `
                <div class="user-marker-body" onclick="handleMarkerEvent('${sourceJson}')">
                  <img class="map-user-icon" src="./static/start-map-icon.png" />
                </div>
                `
        })
        marker._extra = feature.extra;// 附带上当前点位的基本信息，可能其他地方需要对应关系
        var level = feature.level || map.getLevel()
        catchLevelPossibleException();
        var floor = map.getFloor(level)
        marker.addTo(floor)
        return marker
    });
}

function handleMarkerEvent(e) {
    let source = Qs.parse(e)
    console.log(source);
    console.log($parent_this);
}

function handleRiderMarkerEvent(e) {
    let source = e._extra
    console.log("handleRiderMarkerEvent", source, $parent_this);
    let hasIndex = $parent_this.riderList.findIndex(f => f.id == source.id)
    if (hasIndex != -1) {
        let riderItem = $parent_this.riderList[hasIndex]
        if (riderItem.id != $parent_this.activeRiderId) {
            $parent_this.changeRider(riderItem);
            let top = $parent_this.$refs.riderCell[hasIndex].offsetTop
            console.log(top);
            $parent_this.$refs.riderList.scrollTo({
                top: top - 80,
                behavior: "smooth"
            })
        }
    }
}

function removeRiderOrStoreMarkers() {
    if (riderMarkers.length) {
        riderMarkers.forEach(f => {
            f && f.remove();
            f = null;
        })
    }
    riderMarkers = [];
    if (storeMarkers.length) {
        storeMarkers.forEach(f => {
            f && f.remove();
            f = null;
        })
    }
    storeMarkers = [];
}